import java.util.Arrays;

public class Main {
    public static void countSort(int[] array){ //计数排序
        int minVal = array[0];
        int maxVal = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] < minVal){
                minVal = array[i];
            }
            if (array[i] > maxVal){
                maxVal = array[i];
            }
        }
        int[] count = new int[maxVal-minVal+1];
        for (int i = 0; i < array.length; i++) {
            count[array[i]-minVal]++;
        }
        int index = 0;
        for (int i = 0; i < count.length; i++) {
            while (count[i] > 0){
                array[index] = i+minVal;
                index++;
                count[i]--;
            }
        }
    }

    public static void main(String[] args) {
        int[] array = {1,0,9,8,2,3,4,2,3,4,5,2,1};
        countSort(array);
        System.out.println(Arrays.toString(array));
    }
}